import type { FastifyInstance } from "fastify";
import { loginSchema, pingSchema } from "../schemas/index.js";
// import { User } from '../models/index.js';
import { login } from "../services/root.js";
import type { LoginParams } from "../types/index.js";
import { info } from "../utils/logger.js";
import { jsonResponse } from "../utils/response.js";

export default async function (fastify: FastifyInstance, opts) {
	// 验证服务是否启动
	fastify.get("/ping", { schema: pingSchema }, async (request, reply) => {
		return jsonResponse();
	});

	// 登录
	fastify.post("/login", { schema: loginSchema }, async (request, reply) => {
		const params = request.body as LoginParams;
		info({ msg: params, msgPrefix: "login_params", logger: request.log });
		const res = await login(params.username, params.password);
		console.log(res);
		return jsonResponse(res);
	});
}
